﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Globalization;
using System.Text;

namespace ImPM.Application
{
    public sealed class Encryptor
    {
        // Fields
        private string algorithm;
        private byte[] keyBytes;


        // Methods
        public Encryptor(string text)
        {
            ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            encoding.GetBytes(text);

        }

        public Encryptor(byte[] keyBytes)
        {
            this.algorithm = "Rijndael";
            this.keyBytes = keyBytes;
        }

        public Encryptor(byte[] keyBytes, string algorithm)
        {
            this.algorithm = "Rijndael";
            this.keyBytes = keyBytes;
            this.algorithm = algorithm;
        }

        public string Decrypt(string encryptedText)
        {
            string str;
            if (string.IsNullOrEmpty(encryptedText))
            {
                return null;
            }
            if (!encryptedText.StartsWith("{2A48C4DB-F2BF-48DF-A8EF-20F531EA9BAA}:", StringComparison.Ordinal))
            {
                return encryptedText;
            }
            encryptedText = encryptedText.Replace("{2A48C4DB-F2BF-48DF-A8EF-20F531EA9BAA}:", string.Empty);
            string[] strArray = encryptedText.Split(new char[] { '@' });
            if (2 != strArray.Length)
            {
                return null;
            }
            SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create(this.algorithm);
            algorithm.Key = this.keyBytes;
            algorithm.IV = Convert.FromBase64String(strArray[1]);
            ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(strArray[0]));
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
            using (stream2)
            {
                StreamReader reader = new StreamReader(stream2);
                using (reader)
                {
                    str = reader.ReadToEnd();
                }
            }
            return str;
        }

        public string Encrypt(string plaintext)
        {
            if (string.IsNullOrEmpty(plaintext))
            {
                return string.Empty;
            }
            if (plaintext.StartsWith("{2A48C4DB-F2BF-48DF-A8EF-20F531EA9BAA}:", StringComparison.Ordinal))
            {
                return plaintext;
            }
            SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create(this.algorithm);
            algorithm.Key = this.keyBytes;
            algorithm.GenerateIV();
            ICryptoTransform transform = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            using (stream2)
            {
                StreamWriter writer = new StreamWriter(stream2);
                using (writer)
                {
                    writer.Write(plaintext);
                }
            }
            stream.Close();
            byte[] inArray = stream.ToArray();
            return string.Format(CultureInfo.InvariantCulture, "{0}{1}@{2}", new object[] { "{2A48C4DB-F2BF-48DF-A8EF-20F531EA9BAA}:", Convert.ToBase64String(inArray), Convert.ToBase64String(algorithm.IV) });
        }
    }
}
